package cn.boy.service;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import cn.boy.domain.Adminstrator;
import cn.boy.domain.Dormitory;
import cn.boy.domain.Student;
import cn.boy.utils.StreamTool;

public class HomeService {

//	static String basePath = "http://192.168.160.51:8080/Home/servlet/";
//	static String basePath = "http://123.182.49.158:8080/Home/servlet/";
	static String basePath = "http://192.168.89.1:8080/Home/servlet/";
	
	public static String login(String name, String password, String role) throws Exception{
		String path = getRequestPath("LoginServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("name", name);
		params.put("password", password); 
		params.put("role", role);
		return sendPostRequest(path,params,"UTF-8");
	}
	 
	public static String updateInfo(Adminstrator admin) throws Exception{
		String path = getRequestPath("AdminUpdateServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no", admin.getNO());
		params.put("name", admin.getName());
		params.put("age", admin.getAge());
		params.put("sex", admin.getSex());
		params.put("department", admin.getDepartment());
		return sendPostRequest(path,params,"UTF-8");
	}
	
	public static String updateInfo(Student student) throws Exception{
		String path = getRequestPath("StudentUpdateServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no", student.getNO());
		params.put("name", student.getName());
		params.put("age", student.getAge());
		params.put("sex", student.getSex());
		params.put("dormitoryNO", student.getDormitoryNO());
		params.put("classInfo", student.getClassInfo());
		return sendPostRequest(path,params,"UTF-8");
	}
	
	public static String queryDormitoryByPrex(String prex) throws Exception{
		String path = getRequestPath("QueryDormitoryByPrexServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no_prex", prex);
		return sendPostRequest(path,params,"UTF-8");
	}
	
	public static String updateDormitoryMark(Dormitory dorm) throws Exception{
		String path = getRequestPath("UpdateDormitoryMark");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no", dorm.getNO());
		params.put("name", dorm.getName());
		params.put("mark", dorm.getMark());
		params.put("sex", dorm.getDate());
		return sendPostRequest(path,params,"UTF-8");
	}
	
	public static String searchDormitoryMember(String no) throws Exception{
		String path = getRequestPath("SearchDormitoryMemberServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no", no);
		return sendPostRequest(path,params,"UTF-8");
	}
	
	public static String rankDormitory(String prex,String date) throws Exception{
		String path = getRequestPath("RankDormitoryServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no_prex", prex);
		params.put("date", date);
		return sendPostRequest(path,params,"UTF-8");
	}
	
	public static String markHistory(String no) throws Exception{
		String path = getRequestPath("MarkHistoryServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no", no);
		return sendPostRequest(path,params,"UTF-8");
	}
	
	public static String updateAdminPassword(String no,String oldPassword,String newPassword) throws Exception{
		String path = getRequestPath("UpdateAdminPasswordServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no", no);
		params.put("oldPassword", oldPassword);
		params.put("newPassword", newPassword);
		return sendPostRequest(path,params,"UTF-8");
	}
	
	public static String updateStudentPassword(String no,String oldPassword,String newPassword) throws Exception{
		String path = getRequestPath("UpdateStudentPasswordServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no", no);
		params.put("oldPassword", oldPassword);
		params.put("newPassword", newPassword);
		return sendPostRequest(path,params,"UTF-8");
	}
	
	public static String latestMark(String no) throws Exception{
		String path = getRequestPath("LatestMarkServlet");
		Map<String,String> params = new HashMap<String,String>();
		params.put("no", no);
		return sendPostRequest(path,params,"UTF-8");
	}
	
	private static String getRequestPath(String param){
		return basePath + param;
	}
	
	
	public static String sendPostRequest(String path, Map<String, String> params, String enc) throws Exception{
		StringBuilder sb = new StringBuilder();
		if(params!=null && !params.isEmpty()){
			for(Map.Entry<String, String> entry : params.entrySet()){
				sb.append(entry.getKey()).append('=')
					.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
			}
			sb.deleteCharAt(sb.length()-1);
		}
		byte[] entitydata = sb.toString().getBytes();
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5 * 1000);
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
		OutputStream outStream = conn.getOutputStream();
		
		outStream.write(entitydata);
		outStream.flush();
		outStream.close();
		if(conn.getResponseCode()==200){
			InputStream inStream = conn.getInputStream();
			return StreamTool.readInputStream(inStream);
		}
		return "";
	}
	
}
